/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import beans.Avaliation;
import beans.Beer;
import beans.Judge;
import fx.Home;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xml.avaliation.AvaliationReader;
import xml.avaliation.AvaliationWriter;

public class DaoAvaliation implements IDAO<Avaliation> {

    AvaliationWriter writer;
    AvaliationReader reader;
    static Logger logger;

    public DaoAvaliation() {
        writer = new AvaliationWriter(Home.loggedUser);
        reader = new AvaliationReader(Home.loggedUser);
        logger = LoggerFactory.getLogger(DaoAvaliation.class);
    }

    private Long getId() {
        Properties props = new Properties();
        File propsFile = new File("src/xml/avaliation/avaliationSequence.properties");
        boolean needLoadPropsFile = true;

        if (!propsFile.exists()) {
            try {
                propsFile.createNewFile();
                props.load(new FileInputStream(propsFile));
                props.setProperty("lastId", "0");
                props.store(new FileOutputStream(propsFile), "Save the last beer id");
                needLoadPropsFile = false;
            } catch (IOException ex) {
                logger.error("Error to create the properties file", ex);
                return null;
            }
        }

        FileInputStream file;
        try {
            if (needLoadPropsFile) {
                file = new FileInputStream(propsFile);
                props.load(file);
            }
            Long lastId = new Long(props.getProperty("lastId"));
            Long idToSave = lastId + 1;
            props.setProperty("lastId", (idToSave).toString());
            OutputStream fosProps = new FileOutputStream(propsFile);
            props.store(fosProps, "Save the last beer id");
            return lastId;
        } catch (FileNotFoundException ex) {
            logger.error("Sequence file not found", ex);
            return null;
        } catch (IOException ex) {
            logger.error("Error while load or store the properties file", ex);
            return null;
        }
    }

    @Override
    public boolean save(Avaliation entity) {
        if (entity != null && entity.getId() == null) {
            Long id = getId();
            if (id == null) {
                return false;
            } else {
                entity.setId(id);
            }
        }
        HashMap<Judge, List<Avaliation>> allAvaliations = reader.getAllAvaliations();
        for(Avaliation a : allAvaliations.get(entity.getJudge())){
            if(a.equals(entity)){
                logger.warn("This judge already rated this beer");
                return false;
            }
        }

        return writer.print(entity);
    }

    @Override
    public boolean update(Avaliation entity) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean delete(Long id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Avaliation getById(Long id) {
        return reader.getById(id);
    }

    @Override
    public List<Avaliation> getAll() {
        return reader.getAllAvaliationsLoggedUser();
    }

    public HashMap<Judge, List<Avaliation>> getAllAvaliations() {
        return reader.getAllAvaliations();
    }

    public List<Avaliation> getAvaliationsByBeer(Beer beer) {
        List<Avaliation> ret = new ArrayList<>();
        HashMap<Judge, List<Avaliation>> allAvaliations = reader.getAllAvaliations();
        for (Judge j : allAvaliations.keySet()) {
            for (Avaliation a : allAvaliations.get(j)) {
                if (a.getBeer().equals(beer)) {
                    ret.add(a);
                }
            }
        }
        return ret;
    }
}
